#include <QApplication>
#include <QIcon>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include "FileIO.h"
#include "Process.h"

template <typename Class>
static QObject* newSingletonInstance(QQmlEngine *qmlengine, QJSEngine */*jsEngine*/)
{
    return new Class(qmlengine);
}

#ifdef QT_DEBUG
#define IS_DEBUG true
#else
#define IS_DEBUG false
#endif


int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QApplication app(argc, argv);

    app.setOrganizationName("Loentar");
    app.setOrganizationDomain("loentar.personal");
    app.setApplicationName("GI Launcher");
    app.setWindowIcon(QIcon(":/appicon/paimon-launcher.png"));

    QQmlApplicationEngine engine;

    qmlRegisterSingletonType<FileIO>("FileIO", 1, 0, "FileIO", newSingletonInstance<FileIO>);
    qmlRegisterType<Process>("Process", 1, 0, "Process");

    engine.rootContext()->setContextProperty("isDebug", IS_DEBUG);
    engine.rootContext()->setContextProperty("version", VERSION);

    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
        &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}
